package com.utils.oss;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Map;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ListObjectsRequest;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.ObjectListing;

public class UploadSimple {
	private static final String endpoint = " ";
	private static final String accessKeyId  = " ";
	private static final String accessKeySecret  = " ";
	private static final String bucketName = " ";
	
	
/*	private static String date = DateUtil.formatString(new Date(), "yyyyMM");
	private static String datePath = DateUtil.formatString(new Date(), "yyyyMMdd");
	private static String Y = DateUtil.formatString(new Date(), "yyyy");
	private static String M = DateUtil.formatString(new Date(), "MM");
	private static String D = DateUtil.formatString(new Date(), "dd");
	private static String ymd = Y+"/"+M+"/"+D;*/

	public static Map<String, String> transFile(String pathName,Map<String, String> map) throws IOException {
		
/*		File file=new File(pathName);
		String files[]= file.list();
		if(file.isDirectory()){
			for(int i=0;i<files.length;i++){
				if(pathName.endsWith(Const.o)) {
					transFile(pathName+files[i],map);	
				}else {
					transFile(pathName+Const.o+files[i],map);
				}
			}
    	}else {
    		String fileName = file.getName();
    			map.put(fileName, pathName);
    	}*/
		return map;
	}
	/**
	 * 上传图片/视频到阿里云OSS服务器,
	 * @param prefixPath
	 * @param strdate
	 */
	public static void uploadCommon(String prefixPath,String strdate) {/*
		Date date = new Date();
		
		date = DateUtil.formatDate(strdate);
		String Y = DateUtil.formatString(date, "yyyy");
		String M = DateUtil.formatString(date, "MM");
		String D = DateUtil.formatString(date, "dd");
		String ymd = Y+"/"+M+"/"+D;
		
		String prefix = Const.ossJokePre+ymd+Const.o;
		
		String path = "";
		if(prefixPath.endsWith(Const.o)) {
			path = prefixPath+ymd;
		}else {
			path = prefixPath+Const.o+ymd;
		}
		
		Map<String, String> map = new HashMap<>();
 		try {
			transFile(path,map);
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
 		
 		System.out.println("读取的图片/视频总数："+map.size());
 		InputStream inputStream = null;
		OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
    	int index = 0;
 		for (String imageName : map.keySet()) {
 			System.out.println(imageName);
 			System.out.println(map.get(imageName));
			index++;
			try {
				inputStream = new FileInputStream(map.get(imageName));
			} catch (FileNotFoundException e) {
				System.out.println(e.getMessage());
			}
			
			if(inputStream!=null) {
				ossClient.putObject(bucketName, prefix+imageName, inputStream);
			}
		}
		ossClient.shutdown();
 		System.out.println("上传成功的图片/短视频总数："+index);
		
	*/}
	

	public static void main(String[] args) {
/*//		String strdate = "2018-03-19";
		String strdate = DateUtil.formatString(new Date(), DateUtil.YMD_B);
		String picPath = Const.picPath;
		String hotPath = Const.hotPath;
		String videoPath = Const.videoPath;
		System.out.println(strdate);
		
		uploadCommon(picPath,strdate);
		uploadCommon(hotPath,strdate);
		uploadCommon(videoPath,strdate);*/
		
	}
	
	public static void uploadImage() throws FileNotFoundException {
		// 创建OSSClient实例
		OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
		// Object是否存在
		boolean found = ossClient.doesObjectExist(bucketName, "joke/joke/2018/03/15/5aa6e8a8a8def.gif");
		System.out.println(found);
		/*		
		// 列举bucket
		List<Bucket> buckets = ossClient.listBuckets();
		for (Bucket bucket : buckets) {
		    System.out.println(" - " + bucket.getName());
		}
		*/
/*		// 创建bucket
		String bucketName = UploadSimple.bucketName;
		ossClient.createBucket(bucketName);*/
		
		// 上传文件流
/*		InputStream inputStream = new FileInputStream("D:/LocoDown/images/pic/2018/03/15/5aa6e9b35ff99.gif");
		
		ossClient.putObject(bucketName, "joke/joke/2018/03/19/5aa6e9b35ff99.gif", inputStream).getETag();	*/
		// 删除文件 需要权限
//		ossClient.deleteObject(bucketName, "joke/joke/2018/03/15/5aa6e8a8a8def.gif");
		
		//查找是否存在
		boolean found1 = ossClient.doesObjectExist(bucketName, "joke/joke/2018/03/15/5aa6e8a8a8def.gif");
		System.out.println(found1);
		
//		getList(ossClient);
		// 下载object到文件
//		ossClient.getObject(new GetObjectRequest(bucketName, "joke/joke/2018/03/15/5aa6e8a8a8def.gif"), new File("D:/LocoDown/images/pic/2018/03/123.gif"));
		// 关闭client  最后必须关闭
		ossClient.shutdown();
	}	
	
	
	private static void getList(OSSClient ossClient) {
		// 构造ListObjectsRequest请求
		ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucketName);
		// 列出Object 
		ObjectListing listing = ossClient.listObjects(listObjectsRequest);
		// 遍历所有Object
		System.out.println("Objects:");
		for (OSSObjectSummary objectSummary : listing.getObjectSummaries()) {
		    System.out.println(objectSummary.getKey());
		}
		// 遍历所有CommonPrefix
		System.out.println("CommonPrefixs:");
		for (String commonPrefix : listing.getCommonPrefixes()) {
		    System.out.println(commonPrefix);
		}
	}

}
